/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CapadeDatos;

import java.sql.Connection;
import java.sql.Statement;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import modelo.beancondicion;
/**
 *
 * @author principal
 */
public class Daocond {

    public static  beancondicion vercondicion( String x){
         beancondicion lista = new  beancondicion();
    try{
            Connection cn = ConexionBD.getInstance().getConnection();
            Statement stm = cn.createStatement();
            String sql ="Select * from condicion where cod_condicion='"+ x +"';"; // PROCEDIMIENTO ALMACENADO
            ResultSet rs =stm.executeQuery(sql);
            while (rs.next()){
                    beancondicion y  = new  beancondicion();
                   lista.setDescripcion(rs.getString(2));
                   lista.setCod_cond(rs.getString(1));
                   
        }
    }catch(Exception e){System.out.print(e);
   }return lista;
   }

public void insertarcond(String codcond , String desc){
        Boolean verdad = false;
    try{
            Connection cn = ConexionBD.getInstance().getConnection();
            Statement stm = cn.createStatement();
            String sql ="Call proc_registrarcondicion('"
                    + codcond + "', '"
                    + desc +  "','1','');";
            if(stm.executeQuery(sql).wasNull()){
               verdad = false;
               JOptionPane.showMessageDialog(null,"NO INGRESADO");
            }
            else {
                verdad = true;
                JOptionPane.showMessageDialog(null,"INGRESADO"+codcond+"-->"+desc+" ");
            }
    }catch(Exception e){System.out.print(e);
   }
   }
public void modcond(String codcond , String desc,String codmod){
        Boolean verdad = false;
    try{
            Connection cn = ConexionBD.getInstance().getConnection();
            Statement stm = cn.createStatement();
            String sql ="Call proc_registrarcondicion('"
                    + codcond + "', '"
                    + desc +  "','0','"+codmod+"');";
            if(stm.executeQuery(sql).wasNull()){
               verdad = false;
               JOptionPane.showMessageDialog(null,"NO INGRESADO");
            }
            else {
                verdad = true;
                JOptionPane.showMessageDialog(null,"INGRESADO"+codcond+"-->"+desc+" ");
            }
    }catch(Exception e){System.out.print(e);
   if (e.toString().equals("com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '"+codcond+"' for key 'PRIMARY'"))
   {
     JOptionPane.showMessageDialog(null,"NO repetir Condicion"+codcond+"-->"+desc+" ");

   }
    }

}
public static List<beancondicion> listaItem1()   {
       Connection conexion=null;
        int I;
       try {
        //Connection cn = ConexionBD.getInstance().getConnection();
        //Statement stm = cn.createStatement();
        conexion=ConexionBD.getInstance().getConnection();
        String sql="Select cod_condicion,descripcion from condicion";
        Statement st=conexion.createStatement();
        ResultSet rs=st.executeQuery(sql);
        //List<beanmodulo> oListItem=new ArrayList<beanmodulo>();
           ArrayList lista = new ArrayList();
       while(rs.next())
            {
               beancondicion modulo=new beancondicion();
               modulo.setCod_cond(rs.getString(1));
               modulo.setDescripcion(rs.getString(2));
               lista.add(modulo);
            }
        return lista;
       }
       catch(Exception ex)
       {
           ex.printStackTrace();
           return null;
       }
       finally
       {
            try
            {
            conexion.close();
            }
            catch (SQLException ex)
            {
              ex.printStackTrace();
            }
       }
   }

}
